﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer
{
    public class ChangeCollectionDAL
    {
        StationeryInventoryEntities1 ent;

        public ChangeCollectionDAL()
        {
            ent= new StationeryInventoryEntities1();
        }

        public List<CollectionPoint> getCollectionPointList()
        {
            var lst = (from x in ent.CollectionPoints select x);
            List<CollectionPoint> list = lst.ToList<CollectionPoint>();
            return list;
        }

        //public CollectionPointDetail getOneCollectionPoint(int id)
        //{
        //    CollectionPointDetail lst = ent.CollectionPointDetails.FirstOrDefault(x => x.CollectionPointID == id && x.CollectionDate > DateTime.Today);
        //    return lst;
            
        //}

        public List<Employee> saveNewCollectionPointDetail(int id, DateTime colDate, DateTime colTime)
        {
            CollectionPointDetail cp1 = new CollectionPointDetail();
            cp1.CollectionPointID = id;
            cp1.CollectionDate = Convert.ToDateTime(colDate);
            cp1.CollectionTime = Convert.ToDateTime(colTime);
            ent.CollectionPointDetails.Add(cp1);
            ent.SaveChanges();
            int colPointDetId = cp1.CollectionPointDetailID;


            List<Department> deptList = (from x in ent.Departments 
                                         where x.CollectionPointID == id select x).ToList();
            
            List<DeptRequisition> deptReqList = (from x in ent.DeptRequisitions 
                                                 where x.DisbursedBy == null
                                                 select x).ToList();

            List<Employee> repEmail = new List<Employee>();

            foreach (Department d in deptList)
            {
                foreach(DeptRequisition dp in deptReqList)
                {
                    if(d.DeptID == dp.DeptID)
                    {
                        repEmail.Add(d.Employee1);
                        DeptRequisition deptReq = (from x in ent.DeptRequisitions
                                                   where x.DeptID == d.DeptID
                                                   orderby x.DeptReqID descending
                                                   select x).FirstOrDefault();
                        deptReq.CollectionPointDetailID = colPointDetId;
                        ent.SaveChanges();
                    }
                }

            }

            return repEmail;

        }
        public string getColName(int id)
        {
            CollectionPoint cp = ent.CollectionPoints.FirstOrDefault(x => x.CollectionPointID == id);
            return cp.CollectionPointName;
        }

        public DateTime getCollectionTime(int id)
        {
            CollectionPoint cp = ent.CollectionPoints.FirstOrDefault(x => x.CollectionPointID == id);
            return Convert.ToDateTime(cp.CollectionTime);

        }
        public CollectionPointDetail getOneColPointDet(int id)
        {
            CollectionPointDetail cpd = (from x in ent.CollectionPointDetails
                                         where x.CollectionPointID == id
                                         orderby x.CollectionPointDetailID descending
                                         select x).FirstOrDefault();

            return cpd;
        }

        public String getEmailByUsername(String userName)
        {
            Employee e = ent.Employees.FirstOrDefault(x => x.User.UserName == userName);
            return e.Email;
        }

    }
}
